package com.atguigu.java;

/**
 * @author yihu
 * @email 1057202547@qq.com
 * @create 2020-11-2020-11-20-23:53
 * @des
 *   例子：创建三个窗口卖票，总票数为100张.使用实现Runnable接口的方式
 *  * 存在线程的安全问题，待解决。
 */

class Window1 implements  Runnable {

    private  int ticker = 100; //因为只需实例化一个类，所以可以不是static

    @Override
    public void run() {
        while (true){
            if(ticker  > 0){
                System.out.println(Thread.currentThread().getName()+ ": 卖票,票号为：" + ticker);
                ticker--;
            }else {
                break;
            }
        }

    }
}

public class WindowTest1 {
    public static void main(String[] args) {
        Window1 w = new Window1();

        Thread t1 = new Thread(w);
        Thread t2 = new Thread(w);
        Thread t3 = new Thread(w);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }
}


